/**
 * @file		LogManager.h
 * @author		$Author: fantei $
 * @version   	$Rev: 23 $
 * @date       	11 janvier 2013
 * @last		$LastChangedDate: 2012-05-10 15:14:15 +0200 (jeu., 10 mai 2012) $
 * @brief       Control the log Manager
 *
 * @details     This class analyzes the error messages.\n
 * the output can be redirected to the console "debug" by activating\n
 * "setToQdebug (bool)" methode.\n
 * It is possible to save the log to a file with the method "SaveFile(path)".\n
 * You can set the log level with the method "setLevel(level)"\n
 * Important: this class is a Singleton\n
 *
 * First time you can call getInstance(QSTring PathInit, int errorCode)\n
 * currently You can acces by getInstance()\n
 *
 */

#include <QThread>
#include <QtConcurrentRun>
#include <QtConcurrentMap>
#include <QFuture>
#include <QFutureWatcher>
#include <QMutex>
#include <QDir>
#include <QSettings>
#include <type.h>



#ifndef LOGMANAGER_H
#define LOGMANAGER_H


class logmanager: public QThread
{
    Q_OBJECT

// Singleton
private:
    logmanager();
    ~logmanager();

private:
    int SizeIcone;
    int NbLigneMax;
    QString PathFileInit;
    QString DefaultPathFileInit;
    QString PathSaveData;
    bool ToQDebug;
    int ErrorCode;
    int Level;
    int LogNbLine;
    QList<int> CodeError;
    void init();
    QFuture<void> task;

    int SaveConfigFile(QString, QString);



public:

    typedef struct _LogMessage
    {
        UINT32 Number; //!< Message number
        INT32 Type; //!< Message type (SUCCESS,FAILURE,WARNING,CRITICAL,FATAL)
        INT32 Code; //!< Message code
        UINT32 LineNumber; //!< Line number of the source code where the message was formatted
        QString SourceFile; //!< Name of the source code (with full path)
        QString LogMessage; //!< Message text
        TimeStruct LocalTime; //!< Time structure containing the time when the message was formatted
    } LogMessage;
    LogMessage Message;
    QMutex Mutex;



    logmanager(QString);
    void    setErrorCode(int);
    int     ReadConfigFile(QString, QString);
    int     AddMessage(INT32 Type, INT32 Code, UINT32 LineNumber,
            QString SourceFile, QString LogMessage);
    int     ThreadAddMessage(INT32 Type, INT32 Code, UINT32 LineNumber,
            QString SourceFile, QString LogMessage);
    int     getLevel();
    void    setToQdebug(bool);
    QString GetPathFileInit();
    void    SetPathFileInit(QString path);
    int     SaveFile(QString log);


public:
    QString getPathSaveData()
    {
        return PathSaveData;
    }
    ;
    void setPathSaveData(QString val)
    {
        PathSaveData = val;
    }
    ;
    // singleton
public:
    static logmanager *getInstance();
    static logmanager *getInstance(QString pathinit);
    static void kill();
protected:
    static logmanager *singleton;

public slots:
    void setLevel(int);
    void OpenDialLog(void);
    void DisplayMessage(QString line, int code);


signals:
    void DisplayLogMessage(QString line, int code);


};

#endif // LOGMANAGER_H
